package com.gmtx.syb.home.adapter;

import android.content.Context;
import android.support.v4.util.Pools;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.gmtx.syb.application.MyApplication;
import com.gmtx.syb.bean.IndexAdBean;
import com.squareup.picasso.Picasso;

import java.util.List;


/**
 *
 * Created by Administrator on 2016/11/7.
 */

public class RoundPageAdapter extends PagerAdapter implements View.OnClickListener {
    private List<IndexAdBean.ResultBean> list;
    private Pools.Pool<ImageView> pool;
    private Context context;

    public RoundPageAdapter(List<IndexAdBean.ResultBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView view = (ImageView) container.getChildAt(position);
        if (view == null) {
            view = new ImageView(context);
            view.setOnClickListener(this);
        }
        view.setTag(position);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        view.setScaleType(ImageView.ScaleType.FIT_XY);
        container.addView(view,layoutParams);
        Picasso.with(context).load(list.get(position).getImg_url()).into(view);
//        FinalBitmap.create(context).display(view,list.get(position).getImg_url(),
//                ((LMFragmentActivity) context).loadingBitmap, ((LMFragmentActivity) context).loadingBitmap);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public void onClick(View view) {
        int position = (int) view.getTag();
        IndexAdBean.ResultBean indexAd = list.get(position);
        panduan_user(indexAd.getUrl_link(), indexAd.getUrl_title(), indexAd.getPermis(), indexAd.getJump_sign());
    }



    public void panduan_user(String WEB_url, String WEB_title, int permis, String jump_sign) {
        if (permis == 1) {//如果为1的话就必须登录
            if (MyApplication.user.isLogin()) {//如果登陆了就直接进，如果没登陆先登录
                panduan_url(WEB_url, WEB_title, jump_sign);
            } else {
                //((LMFragmentActivity) context).startLMActivity(NewLoadingActivity.class);
            }
        } else {
            panduan_url(WEB_url, WEB_title, jump_sign);
        }
    }

    /**
     * 专门为轮播图设计的，判断他们走那个webview
     * <p/>
     * WEB_url     跳转的url
     * WEB_title   跳转的名称
     * jump_sign   Y的话跳转普通商城的webview，N的话就不显示导航的webview，T的话就跳转到天猫的webview
     */
    public void panduan_url(String WEB_url, String WEB_title, String jump_sign) {

        if (WEB_url.indexOf("activity://") != -1) {
            //((LMFragmentActivity) context).toast("开发中敬请期待！");

        } else {
//            Public_mode public_mode = new Public_mode();
//            public_mode.url = WEB_url;
//            public_mode.context = WEB_title;
//            if (jump_sign.equals("Y")) {// 这个是显示普通商城的导航的webview
////            ACWebUrl(WEB_url, WEB_title, permis);
//            } else if (jump_sign.equals("N")) {// 不显示导航的webview
//                ((LMFragmentActivity) context).startLMActivity(OneWebActivity.class, public_mode);
//            } else {// 显示天猫导航的webview
//                LMTool.tiaozhuan(public_mode);
//            }
        }
    }
}
